package OddMuse::Date;

=head1 NAME

OddMuse::Database - Perl Module for Date Manipulation

=head1 DESCRIPTION

This module provides some more human readable date functions, some of which are
used by other modules.

=cut

$VERSION = ( q$Revision: 38 $ ) =~ /(\d+)/g;
use strict;

=head1 SYNOPSIS

  use OddMuse::Date;
  &OddMuse::Date::CustomCalcDay( 'an epoch' );
  &OddMuse::Date::TimeToRFC822( 'an epoch' );
  &OddMuse::Date::LongTimeSince( 'an epoch' );

=head2 FUNCTIONS

=over 4

=item * CustomCalcDay( time );

Returns a string in the format... "Thursday, April 12th 2001 at 7:33 AM"

=cut

sub CustomCalcDay {
    use Date::Format;
    my $doodad = time2str( "%A, %B %o %Y at %l:%M %p", $_[0] );
  return $doodad;

    # my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
    # return sprintf('%4d-%02d-%02d', $year+1900, $mon+1, $mday);
} ## end sub CustomCalcDay

=item * TimeToRFC822( time );

Needed to squash the OddMuse native function because the introduction of
Time::Format mucks with strftime.  Returns the same string oddmuse's original
function.

=cut

sub TimeToRFC822 {
  return time2str( "%a, %d %b %Y %T GMT", $_[0] );

#return strftime "%a, %d %b %Y %T GMT", gmtime(shift); #   Sat, 07 Sep 2002 00:00:01 GMT
}

=item * LongTimeSince( time );

Returns a human readable string of time elapsed since the time input.  It
returns two "clumps" of date data, for easy reading.  So depending on the
amount of time passed since the time input string, you'll get a result like:

  "5 years, 5 months" 
  "1 day, 9 hours"
  "1 minute, 3 seconds"

=cut

sub LongTimeSince {
    my $comparetime = $_[0];
    my $epochtime   = time;

    my ( $temp, $yearsince, $remainder, $since, @datestring, @alldone );

    my $datecount = 0;
    my $depth     = 2;

    if ( $epochtime gt $comparetime ) {
        $since = $epochtime - $comparetime;
    } else {
        $since = $comparetime - $epochtime;
    }

    my $year  = 60 * 60 * 24 * 365;
    my $month = 60 * 60 * 24 * 30;
    my $week  = 60 * 60 * 24 * 7;
    my $day   = 60 * 60 * 24;
    my $hour  = 60 * 60;
    my $min   = 60;

    my $years = sprintf( "%.1d", $since / $year );
    $remainder = $since - ( $years * $year );

    if ( ( $years ne 0 ) && ( $datecount < $depth ) ) {
        if ( $years eq 1 ) {
            push( @datestring, "$years year" );
        } else {
            push( @datestring, "$years years" );
        }
        $datecount++;
    } ## end if ( ( $years ne 0 ) &&...
    my $months = sprintf( "%.1d", $remainder / $month );
    $remainder = $remainder - ( $months * $month );

    if ( ( $months ne 0 ) && ( $datecount < $depth ) ) {
        if ( $months eq 1 ) {
            push( @datestring, "$months month" );
        } else {
            push( @datestring, "$months months" );
        }
        $datecount++;
    } ## end if ( ( $months ne 0 ) ...

    my $weeks = sprintf( "%.1d", $remainder / $week );
    $remainder = $remainder - ( $weeks * $week );

    if ( ( $weeks ne 0 ) && ( $datecount < $depth ) ) {
        if ( $weeks eq 1 ) {
            push( @datestring, "$weeks week" );
        } else {
            push( @datestring, "$weeks weeks" );
        }
        $datecount++;
    } ## end if ( ( $weeks ne 0 ) &&...

    my $days = sprintf( "%.1d", $remainder / $day );
    $remainder = $remainder - ( $days * $day );

    if ( ( $days ne 0 ) && ( $datecount < $depth ) ) {
        if ( $days eq 1 ) {
            push( @datestring, "$days day" );
        } else {
            push( @datestring, "$days days" );
        }
        $datecount++;
    } ## end if ( ( $days ne 0 ) &&...

    my $hours = sprintf( "%.1d", $remainder / $hour );
    $remainder = $remainder - ( $hours * $hour );
    if ( ( $hours ne 0 ) && ( $datecount < $depth ) ) {
        if ( $hours eq 1 ) {
            push( @datestring, "$hours hour" );
        } else {
            push( @datestring, "$hours hours" );
        }
        $datecount++;
    } ## end if ( ( $hours ne 0 ) &&...

    my $mins = sprintf( "%.1d", $remainder / $min );
    $remainder = $remainder - ( $mins * $min );

    if ( ( $mins ne 0 ) && ( $datecount < $depth ) ) {
        if ( $mins eq 1 ) {
            push( @datestring, "$mins minute" );
        } else {
            push( @datestring, "$mins minutes" );
        }
        $datecount++;
    } ## end if ( ( $mins ne 0 ) &&...

    if ( ( $remainder ne 0 ) && ( $datecount < $depth ) ) {
        if ( $remainder eq 1 ) {
            push( @datestring, "$remainder second" );
        } else {
            push( @datestring, "$remainder seconds" );
        }
        $datecount++;
    } ## end if ( ( $remainder ne 0...

    $temp = pop( @datestring );
    foreach my $date ( @datestring ) {
        push( @alldone, $date . ", " );
    }
    push( @alldone, $temp );

  return "@alldone\n";
} ## end sub LongTimeSince
1;

__END__

=back

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<cmauch@gmail.com>

=head1 AUTHOR

Charles Mauch <cmauch@gmail.com>

=head1 LICENSE

Copyright (c) 2006 Charles Mauch

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 SEE ALSO

perl(1).  OddMuse(1).

=cut

# $Id: Date.pm 38 2006-10-01 04:06:35Z cmauch $
